package org.xenon.vote.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(filterName = "LoginFilter", urlPatterns = "/*")
public class LoginFilter implements Filter {

    public void destroy(){
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        String uri = request.getRequestURI();
        if (!uri.equals("/ballot/status") && !uri.equals("/turn/prepare")  && !uri.equals("/judge/status")){
            System.out.println(uri+"\t"+request.getSession().getId() + "\t访问了");
        }
        if(uri.contains(".html")){
            resp.setContentType("text/html;charset=utf-8");
            chain.doFilter(req, resp);
            return;
        }
        if(uri.contains(".js")){
            resp.setContentType("application/javascript;charset=utf-8");
            chain.doFilter(req, resp);
            return;
        }
        if(uri.contains(".css")){
            resp.setContentType("text/css;charset=utf-8");
            chain.doFilter(req, resp);
            return;
        }
        resp.setContentType("text/html;charset=utf-8");
        if (    uri.contains("/user/register") ||
                uri.contains("/user/recovery") ||
                uri.contains("/captcha") ||
                uri.contains("/user/check") ||
                uri.contains("/") ||
                uri.contains("/favicon.ico") ||
                uri.contains("/judge/login") ||
                uri.contains("/user/login")) {
            chain.doFilter(req, resp);
            return;
        } else //noinspection StatementWithEmptyBody
            if (request.getSession().getAttribute("user") != null) {
                chain.doFilter(req, resp);
            return;
        } else {
            //cookie免登录
            /*Cookie[] cookies = request.getCookies();
            for (Cookie c : cookies) {
                if (c.getName().equals("login")) {
                    if ((userName = loginCheck(c.getValue())) != null) {
                        OnlineJudgeListener.login();
                        chain.doFilter(req, resp);
                        return;
                    }
                    break;
                }
            }*/
        }
        ((HttpServletResponse) resp).sendError(403, uri);
    }

    public void init(FilterConfig config) {
    }

/*    private String loginCheck(String cookie) {
        Login login = loginDao.get(cookie);
        if (login == null) return null;
        return login.getMail();
    }*/
}






















/*
package org.xenon.vote.filter;

import org.xenon.vote.dao.LoginDAO;
import org.xenon.vote.model.bo.OnlineMarker;
import org.xenon.vote.model.pojo.po.Login;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Timer;
import java.util.TimerTask;

@WebFilter(filterName = "LoginFilter", urlPatterns = "/*")
public class LoginFilter implements Filter {
    private static HashSet<String> onlineUser;//用户的SessionID
    private static HashSet<String> onlineJudge;//裁判的SessionID
    private static LinkedList<String> userList;//用户的SessionID
    private static LinkedList<String> judgeList;//裁判的SessionID
    private static Timer timer;//计时器
    String encoding;

    public void destroy() {
        encoding = null;
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        String uri = request.getRequestURI();
        System.out.println(uri);
        String userName;
        if (encoding != null) {
            request.setCharacterEncoding("utf-8");
            resp.setContentType("text/html;charset=" + encoding);
        }
        if (uri.equals("/user/register") ||
                uri.equals("/user/recovery") ||
                uri.startsWith("/user/check")) {
            chain.doFilter(req, resp);
            return;
        }
        else if (uri.equals("/user/login")) {
            chain.doFilter(req, resp);
            if (req.getAttribute("loginStatus") != null && req.getAttribute("loginStatus").equals("true")) {
                onlineUser.add(((HttpServletRequest) req).getSession().getId());
                userList.add(((HttpServletRequest) req).getSession().getId());
            }
            return;
        }
        else if (uri.equals("/judge/login")) {
            chain.doFilter(req, resp);
            if (req.getAttribute("judgeLogin") != null && req.getAttribute("judgeLogin").equals("true")) {
                onlineJudge.add(((HttpServletRequest) req).getSession().getId());
                judgeList.add(((HttpServletRequest) req).getSession().getId());
            }
            return;
        }
        else if ((userName = OnlineMarker.find(request.getSession().getId())) != null) {
            if (uri.equals("/user/logout")) {
                onlineUser.remove(((HttpServletRequest) req).getSession().getId());
                userList.remove(((HttpServletRequest) req).getSession().getId());
                ((HttpServletRequest) req).getSession().invalidate();
            }
            chain.doFilter(req, resp);
            System.out.println(((HttpServletRequest) req).getSession().getId());
            return;
        }
        else {
            Cookie[] cookies = request.getCookies();
            for (Cookie c : cookies) {
                if (c.getName().equals("login")) {
                    if (loginCheck(c.getValue())) {
                        onlineUser.add(((HttpServletRequest) req).getSession().getId());
                        userList.add(((HttpServletRequest) req).getSession().getId());
                        chain.doFilter(req, resp);
                        return;
                    }
                    break;
                }
            }
        }
        ((HttpServletResponse) resp).sendError(403, "服务器理解了你的意思但是拒绝提供服务!");
    }

    public void init(FilterConfig config) {
        onlineUser = new HashSet<>();
        onlineJudge = new HashSet<>();
        userList = new LinkedList<>();
        judgeList = new LinkedList<>();
        this.encoding = "utf-8";
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if (userList.size() > 5000)
                    for (int i = 5000; i < userList.size(); i++)
                        onlineUser.remove(userList.pop());
                if (judgeList.size() > 5000)
                    for (int i = 5000; i < judgeList.size(); i++)
                        onlineJudge.remove(judgeList.pop());
            }
        }, 1000*60*10L, 1000*60*10L);
    }

    private boolean loginCheck(String cookie) {
        Login login = LoginDAO.get(cookie);
        return login != null;
    }

    private void loginUser(String cookie) {

    }

    private void loginJudge(String cookie) {

    }


}
*/
